//
//  HomeMessageViewController.m
//  GC_OC
//
//  Created by 曾 on 2022/1/16.
//

#import "HomeMessageViewController.h"
#import "HomeMessageTableViewCell.h"
#import "MessagePopView.h"

@interface HomeMessageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)ObjectAFNetWorking* objectAFN;
@property (nonatomic, strong)UITableView* tableview;
@property (nonatomic, strong)NSArray* datas;
@property (nonatomic, weak) MessagePopView *pop;

@end

@implementation HomeMessageViewController

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    if ([SUPlayerManager manager].currentSong) {
        CGFloat height = 0;
        if (@available(iOS 13.0, *)) {
            NSSet *set = [UIApplication sharedApplication].connectedScenes;
            UIWindowScene *windowScene = [set anyObject];
            UIWindow *window = windowScene.windows.firstObject;
            height = window.safeAreaInsets.bottom;
        } else if (@available(iOS 11.0, *)) {
            UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
            height = window.safeAreaInsets.bottom;
        }
        height += 49;

        [[SUPlayerManager manager].bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo([UIApplication sharedApplication].keyWindow);
            make.bottom.equalTo([UIApplication sharedApplication].keyWindow).offset(-height+49);
            make.height.mas_equalTo(60);
        }];
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.objectAFN=[[ObjectAFNetWorking alloc]init];
    
    _tableview=[[UITableView alloc]initWithFrame:CGRectMake(0,topHeight,SCREEN_WIDTH,SCREEN_HEIGHT) style:UITableViewStylePlain];
    _tableview.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
    _tableview.dataSource=self;
    _tableview.delegate=self;
    [self.view addSubview:_tableview];
    _tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
//    _tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [_tableview.mj_header beginRefreshing];
    [self creatHeadView];
    
    MessagePopView *pop = [[MessagePopView alloc] initWithFrame:CGRectMake(0, topHeight, fkScreenW, 0)];
    [self.view addSubview:pop];
    self.pop = pop;
}


-(void)creatHeadView
{
    UIView* view=[[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,topHeight)];
    view.backgroundColor=AppMainColor;
    CGFloat widht=[GC_OCTool getWidthForLabel:@"一键清空" andFont:[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(12)] andHeight:wid(100)]+wid(40);
    CGFloat titleHeight=[GC_OCTool getHeightForLabel:@"A" andFont:[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(17)] andWidth:wid(100)];
    UILabel* titleLabel=[[UILabel alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-(SCREEN_WIDTH-wid(65)-widht))/2.0,StatusBarHeight+(topHeight-StatusBarHeight-titleHeight)/2.0,SCREEN_WIDTH-wid(65)-widht,titleHeight)];
    titleLabel.textColor=[UIColor whiteColor];
    [titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(17)]];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.text=@"消息栏";
    [view addSubview:titleLabel];
    
    UIButton* leftbtn=[UIButton new];
    leftbtn.frame=CGRectMake(wid(20),StatusBarHeight+(topHeight-StatusBarHeight-wid(40))/2.0,wid(40),wid(40));
    [leftbtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftbtn addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton* rightbtn=[UIButton new];
    rightbtn.frame=CGRectMake(SCREEN_WIDTH-widht,StatusBarHeight+(topHeight-StatusBarHeight-wid(40))/2.0,widht,wid(40));
    [rightbtn setTitle:@"一键清空" forState:UIControlStateNormal];
    [rightbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightbtn.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(12)];
    [rightbtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:rightbtn];
    [view addSubview:leftbtn];
    [self.view addSubview:view];
}

#pragma mark - tableview delegate and datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeMessageTableViewCell* cell=[HomeMessageTableViewCell cellWith:tableView];
    cell.data = self.datas[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return hig(120);
}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    id data = self.datas[indexPath.row];
    [self.pop setData:data];
    // 执行打开的动画
    [self.pop openWith:fkScreenH - topHeight];
}
-(void)leftBtnAction
{
//    HomeMessageViewController* homeMessage=[[HomeMessageViewController alloc]init];
//    CATransition* transition = [CATransition animation];
//    transition.type = kCATransitionPush;//可更改为其他方式
//    transition.subtype = kCATransitionFromRight;//可更改为其他方式
//    transition.duration = 0.25f;
////    transition.time
//    homeMessage.hidesBottomBarWhenPushed=YES;
//    [self.navigationController.view.layer addAnimation:transition forKey:kCAOnOrderIn];
    [self.navigationController popViewControllerAnimated:YES];
    if (self.navigationController.viewControllers.count == 1) {
        if ([ICMusicPlayManager sharedManager].narrowView) {
            [ICMusicPlayManager sharedManager].narrowView.frame = CGRectMake(0, SCREEN_HEIGHT - 50 - GCtabbarHeight, SCREEN_WIDTH, 50);
        }
    }
}
-(void)rightBtnAction
{

}

#pragma mark - 网络请求
-(void)loadData
{
//    [self.objectAFN getUrlStr:@"" finish:^(id  _Nonnull dataString) {
//
//    } andError:^(NSError * _Nonnull error) {
//
//    }];
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableview.mj_header endRefreshing];
        // 模拟测试数据
        self.datas = @[
            @{@"title": @"这是标题", @"content": @"这是内容，这是个很长的文字这是个很长的文字这是个很长的文字这是个很长的文字这是个很长的文字这是个很长的文字这是个很长的文字这是个很长的文字这是个很长的文字这是个很长的文字。"},
            @{@"title": @"这是标题1", @"content": @"这是内容"},
            @{@"title": @"这是标题2", @"content": @"这是内容"},
            @{@"title": @"这是标题3", @"content": @"这是内容"}
        ];
        [self.tableview reloadData];
    });
}

@end
